/**
import {debounce,throttle} from "@/utils/common.js";
Vue.prototype.$debounce = debounce;
handletext() {
	//防抖失败
	this.$debounce(() => {
		console.log("测试");
		console.log(this.xxxx);
	})()
}
**/
/**
 * 一个一个引入
 import {debounce,throttle} from "@/utils/common.js";
 handletext: debounce(function(){
	 //可以用this
 })
 * */


/**
 * @desc 函数防抖
 * @param func 目标函数
 * @param wait 延迟执行毫秒数
 * @param immediate true - 立即执行， false - 延迟执行
 */
export const debounce = function(func, wait = 1000, immediate = true) {
	let timer;
	return function() {
		let context = this,
			args = arguments;
		if (timer) clearTimeout(timer);
		if (immediate) {
			let callNow = !timer;
			timer = setTimeout(() => {
				timer = null;
			}, wait);
			if (callNow) func.apply(context, args);
		} else {
			timer = setTimeout(() => {
				func.apply(context, args);
			}, wait)
		}
	}
}
/**
 * @desc 函数节流
 * @param func 函数
 * @param wait 延迟执行毫秒数
 * @param type 1 使用表时间戳，在时间段开始的时候触发 2 使用表定时器，在时间段结束的时候触发
 */
export const throttle =function (func, wait = 1000, type = 1) {
	let previous = 0;
	let timeout;
	return function() {
		let context = this;
		let args = arguments;
		if (type === 1) {
			let now = Date.now();

			if (now - previous > wait) {
				func.apply(context, args);
				previous = now;
			}
		} else if (type === 2) {
			if (!timeout) {
				timeout = setTimeout(() => {
					timeout = null;
					func.apply(context, args)
				}, wait)
			}
		}
	}
}


/**返回唯一数
 *@param {number} [len=16]  位数
 *@param {number} [radix=62]  进制
 */
export const uniqueE = function(len = 16, radix = 62) {
	if (typeof len !== 'number' || typeof radix !== 'number') throw new Error('not a number.');

	len = len < 8 ? 8 : len > 32 ? 32 : len;
	radix = radix < 6 ? 6 : radix > 62 ? 62 : radix;

	let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
		uuid = [],
		i;

	// 把字符串变为数组
	chars = chars.split('');
	// 打乱数组顺序/使其变为无序
	chars = chars.sort(() => Math.random() - 0.5);

	if (len) {
		for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
	} else {
		let r = undefined;

		uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
		uuid[14] = '4';

		for (i = 0; i < 36; i++) {
			if (!uuid[i]) {
				r = 0 | Math.random() * 16;
				uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
			}
		}
	}

	return uuid.join('');
}